/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 jfinal app. jfapp Group.
 */

package app.controllers.mobileweb;

import app.Const;
import app.dtos.FloorWidthBrand;
import app.dtos.FormatWidthBrand;
import app.dtos.Goods;
import app.dtos.ShowBrand;
import app.events.EventBusContext;
import app.events.stat.BrandClickEvent;
import app.interceptors.MobileWxMemberInterceptor;
import app.models.activity.BrandActivity;
import app.models.activity.BrandActivityComment;
import app.models.activity.BrandActivityLike;
import app.models.activity.BrandActivityShare;
import app.models.brand.Brand;
import app.models.brand.BrandAttention;
import app.models.brand.BrandFormat;
import app.models.brand.BrandStat;
import app.models.brand.Commodity;
import app.models.info.Floor;
import com.github.sog.config.JFinalApp;
import com.github.sog.config.StringPool;
import com.github.sog.controller.AjaxMessage;
import com.github.sog.controller.BasicController;
import com.github.sog.kit.servlet.RequestKit;
import com.github.sog.libs.AppFunc;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by devil
 * 品牌汇
 */
@Before(MobileWxMemberInterceptor.class)
public class BrandController extends BasicController {

    /**
     * 品牌列表
     */
    public void index() {

        String type = getPara("type");
        setAttr("type", Strings.isNullOrEmpty(type) ? "brand" : type);
        List<FormatWidthBrand> list = Lists.newArrayList();

        // format
        List<BrandFormat> styles = BrandFormat.dao.list();
        FormatWidthBrand data;
        for (BrandFormat item : styles) {
            List<Brand> brands = Brand.dao.find("select id, name, e_name, logo from ol_brand where format= ? limit 6", item.getInt("id"));

            data = new FormatWidthBrand();
            data.BrandList = brands;
            data.BrandFormat = item;
            list.add(data);
        }
        setAttr("brandList", list);

        List<FloorWidthBrand> floorList = Lists.newArrayList();

        List<Floor> floors = Floor.dao.findByNotParking();
        FloorWidthBrand floorData;
        for (Floor item : floors) {
            List<Brand> brands = Brand.dao.find("select id, name, e_name, logo from ol_brand where floor= ? limit 6", item.getInt("id"));

            floorData = new FloorWidthBrand();
            floorData.BrandList = brands;
            floorData.Floor = item;
            floorList.add(floorData);
        }
        setAttr("brandList", list);
        setAttr("floorBrandList", floorList);
        render("brand.ftl");
    }

    /**
     * 品牌详情
     */
    public void showbrand() {
        long brandid = getParaToLong(AppFunc.TABLE_PK_COLUMN, 0l);
        long mid = getParaToLong(Const.MEMBER_PARAM, 0l);
        ShowBrand sb = Brand.dao.findWithStatById(brandid);
        BrandAttention attention = BrandAttention.dao.findByMemberAndBrand(mid, brandid);
        sb.attention = attention != null;

        if (sb.picture != null) {
            List<String> pics = AppFunc.COMMA_SPLITTER.splitToList(sb.picture);
            if (pics != null && pics.size() > 4) {
                setAttr("pics", pics.subList(0, 4));
            } else {
                setAttr("pics", pics);
            }
        }

        List<BrandActivity> activitys = BrandActivity.dao.findByBrandId(brandid);
        sb.activitys = activitys;
        if (activitys != null && !activitys.isEmpty()) {
            setAttr("activity", activitys.get(0));
        }

        EventBusContext.post(BrandClickEvent.create(brandid, mid, getRequest()));

        setAttr("sb", sb);
        setAttr("id", brandid);
        render("showbrand.ftl");
    }

    /**
     * 关注
     */
    public void follow() {
        long brandid = getParaToLong("id", 0l);
        long mid = getParaToLong(Const.MEMBER_PARAM, 0l);
        String wxno = getPara(Const.WX_PARAM);

        int n = BrandAttention.dao.onOrUnAttention(brandid, mid, wxno);

        int cur = BrandStat.dao.follow(brandid, mid, n);
        renderJson(AjaxMessage.ok("ok", cur));
    }

    /**
     * 百货列表
     */
    public void departmentstore() {
        render("departmentstore.ftl");
    }

    /**
     * 品牌更多列表
     */
    public void brandmore() {
        long id = getParaToLong("id", 0l);
        if (id > 0) {
            String type = getPara("type");
            String name = StringPool.EMPTY;
            if (StringUtils.equals("brand", type)) {
                BrandFormat style = BrandFormat.dao.findById(id);
                List<Brand> brands = Brand.dao.findByFormat(id);
                setAttr("list", brands);
                name = style.getStr("name");
            } else if (StringUtils.equals("floor", type)) {
                Floor floor = Floor.dao.findById(id);
                List<Brand> brands = Brand.dao.findbyFloor(id);
                name = floor.getStr("name");
                setAttr("list", brands);
            }
            setAttr("name", name);
            render("brandmore.ftl");
        } else {
            renderNull();
        }
    }

    /**
     * 商品列表
     */
    public void goods() {
        String brandid = getPara("id");
        Brand brand = Brand.dao.findById(brandid);
        List<Commodity> commodities = Commodity.dao.findByBrandId(brandid);

        Goods goods = new Goods();
        goods.brandName = brand.getStr("name");
        goods.commodityList = commodities;

        if (brand.getStr("picture") != null) {
            goods.pictures = AppFunc.COMMA_SPLITTER.splitToList(brand.getStr("picture"));
        }

        setAttr("goods", goods);
        render("goods.ftl");
    }

    /**
     * 品牌活动
     */
    public void activity() {
        long brandid = getParaToLong("brandid", 0l);
        long mid = getParaToLong("mid", 0l);
        List<Record> list;
        if (brandid == 0) { //查询所有的品牌活动
            list = Db.find(SqlKit.sql("brandactivity.findAllWithBrand"), mid);
        } else { //查询当前活动
            list = Db.find(SqlKit.sql("brandactivity.findWithBrandByBrandId"), brandid, mid);
        }

        setAttr("domain", JFinalApp.domain);

        setAttr("brandid", brandid);
        setAttr("list", list);
        render("activity.ftl");
    }

    /**
     * 品牌活动分享
     */
    public void activityShare() {
        long mid = getParaToLong(Const.MEMBER_PARAM, 0l);
        String wxno = getPara(Const.MEMBER_PARAM);
        long brand = getParaToLong("brand", 0l);
        long activity = getParaToLong("activity", 0l);

        // Db.update(SqlKit.sql("brandactivityshare.deleteByActivityAndMember"), activity, mid);

        BrandActivityShare share = new BrandActivityShare();
        share.set("share_time", new Date());
        share.set("member", mid);
        share.set("wx_account", wxno);
        share.set("create_time", new Date());
        share.set("activity", activity);
        share.save();
        renderJson(AjaxMessage.ok("分享成功"));
    }

    /**
     * 品牌活动喜欢(赞)
     */
    public void activityLike() {
        long mid = getParaToLong(Const.MEMBER_PARAM, 0l);
        String wxno = getPara(Const.WX_PARAM);
        long brand = getParaToLong("brand", 0l);
        long activity = getParaToLong("activity", 0l);

        Db.update(SqlKit.sql("brandactivitylike.deleteByActivityAndMember"), activity, mid);

        BrandActivityLike like = new BrandActivityLike();
        like.set("like_time", new Date());
        like.set("member", mid);
        like.set("wx_account", wxno);
        like.set("create_time", new Date());
        like.set("activity", activity);
        like.save();

        renderJson(AjaxMessage.ok(like.get("id")));
    }

    /**
     * 取消品牌活动喜欢(赞)
     */
    public void unActivityLike() {
        long likeId = getParaToLong("likeId", 0l);
        Db.deleteById("ol_brand_activity_like", likeId);
        renderJson(AjaxMessage.ok("取消喜欢成功"));
    }

    /**
     * 打开朋友圈分享的活动
     */
    public void activityInfo() {
        long activity = getParaToLong("activity", 0l);
        Record record = Db.findFirst(SqlKit.sql("brandactivity.findWithBrandById"), activity);

        String s = record.getStr("activityPicture");
        if (!Strings.isNullOrEmpty(s)) {
            List<String> pictures = AppFunc.COMMA_SPLITTER.splitToList(s);
            setAttr("pictures", pictures);
        }

        setAttr("record", record);
        setAttr("domain", JFinalApp.domain);
        render("activity_info.ftl");
    }

    /**
     * 评论
     */
    public void activityReply() {
        long mid = getParaToLong(Const.MEMBER_PARAM, 0L);
        String wxno = getPara(Const.WX_PARAM);
        String content = getPara("content");
        long activity = getParaToLong("activity", 0l);
        if (activity == 0) {
            renderJson(AjaxMessage.failure("没有品牌活动"));
            return;
        }

        Date date = new Date();
        String clientIp = RequestKit.getRemoteAddr(getRequest());
        BrandActivityComment comment = new BrandActivityComment();
        comment.set("member", mid);
        comment.set("wx_account", wxno);
        comment.set("comment_time", date);
        comment.set("content", content);
        comment.set("client_ip", clientIp);
        comment.set("create_time", date);
        comment.set("activity", activity);
        comment.save();
        renderJson(AjaxMessage.ok(StringPool.EMPTY, content));
    }

    public void loadCommentAndLike() {
        long activity = getParaToLong("activity", 0l);

        long likeCount = BrandActivityLike.dao.countList(activity);
        List<BrandActivityComment> comments = BrandActivityComment.dao.findByActivity(activity);
        setAttr("likeCount", likeCount);
        setAttr("comments", comments);
        render("loadCommentAndLike.ftl");
    }

    public void road() {

        long brand_id = getParaToLong(0, 0l);
        if (brand_id == 0) {
            renderNull();
        } else {
            Brand brand = Brand.dao.findWithFloorById(brand_id);
            if (brand == null) {
                renderNull();
            } else {
                setAttr("brand", brand);
            }
        }
    }

}
